<?php

namespace App\CustHub\Controller\Admin;

use App\CustHub\Controller\BaseController;
use App\CustHub\Request\ServerRequest;
use App\CustHub\Service\ChServerService;
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\DeleteMapping;
use Hyperf\HttpServer\Annotation\GetMapping;
use Hyperf\HttpServer\Annotation\PostMapping;
use Hyperf\HttpServer\Annotation\PutMapping;
use Mine\Annotation\Auth;
use Mine\Annotation\OperationLog;
use Mine\Annotation\Permission;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
use Psr\Http\Message\ResponseInterface;

#[Controller(prefix: "ch/admin/server"),Auth]
class ChServerController extends BaseController
{
    #[Inject]
    protected ChServerService $service;

    /**
     * 获取服务器列表
     * @return ResponseInterface
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    #[GetMapping('index'),Permission("ch:admin:server:index")]
    public function getServerList(): ResponseInterface
    {
        return $this->success($this->service->getServerList($this->request->all()));
    }

    /**
     * 新增服务器
     * @param ServerRequest $request
     * @return ResponseInterface
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    #[PostMapping("save"), Permission("ch:admin:server:save"), OperationLog]
    public function save(ServerRequest $request): ResponseInterface
    {
        return $this->success(['id' => $this->service->save($request->all())]);
    }

    /**
     * 更新服务器
     * @param int $id
     * @param ServerRequest $request
     * @return ResponseInterface
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    #[PutMapping("update/{id}"), Permission("ch:admin:server:update"), OperationLog]
    public function update(int $id, ServerRequest $request): ResponseInterface
    {
        return $this->service->update($id, $request->all()) ? $this->success() : $this->error();
    }

    /**
     * 单个或批量删除服务器
     * @return ResponseInterface
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    #[DeleteMapping("delete"), Permission("ch:admin:server:delete"), OperationLog]
    public function delete(): ResponseInterface
    {
        $requestData = $this->request->input('ids', []);
        return $this->service->delete($requestData['ids']) ? $this->success() : $this->error();
    }

    /**
     * 获取省市列表
     * @return ResponseInterface
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */

    #[PostMapping('getSecondaryTreeList')]
    public function getSecondaryTreeList(): ResponseInterface
    {
        return $this->success('地区二级树形结构', $this->service->getSecondaryTreeList());
    }

}